package com.woniuxy.usercoachassemble.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.usercoachassemble.commons.PageReq;
import com.woniuxy.usercoachassemble.commons.ToData;
import com.woniuxy.usercoachassemble.commons.ValidatorUtils;
import com.woniuxy.usercoachassemble.dao.*;
import com.woniuxy.usercoachassemble.entity.*;
import com.woniuxy.usercoachassemble.service.CoachInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2020-07-09
 */
@Service
public class CoachInfoServiceImpl extends ServiceImpl<CoachInfoMapper, CoachInfo> implements CoachInfoService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserinfoMapper userinfoMapper;

    @Autowired
    private CoachInfoMapper coachInfoMapper;
    @Autowired
    private AccountMapper accountMapper;

    @Override
    public ToData selectCoachInfoByDynamic(String nickname, String username, String tel,Long current,Long size) {

        User user = null;
        ToData toData = new ToData();
        List<Userinfo> userinfoList = null;

        if(!ValidatorUtils.isNullOrEmpty(tel)){
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<User>();
            userQueryWrapper.eq("tel",tel);
            user = userMapper.selectOne(userQueryWrapper);

            PageReq pageReq = new PageReq();
            pageReq.setCurrent(current);
            pageReq.setSize(size);

            if(user != null){
                Userinfo userinfo = userinfoMapper.selectOne(new QueryWrapper<Userinfo>().eq("user_id",user.getUserId()));
                userinfo.setUser(user);
                userinfo.setCoachInfo(query().eq("user_id",user.getUserId()).one());
                userinfoList = new ArrayList<Userinfo>();
                userinfoList.add(userinfo);

                pageReq.setTotal((long)1);
                pageReq.setPages((long)1);
                toData.setData(userinfoList);

                toData.setPageReq(pageReq);
                return toData;
            }

            pageReq.setTotal((long)0);
            pageReq.setPages((long)0);

            userinfoList = new ArrayList<>();
            toData.setData(userinfoList);
            toData.setPageReq(pageReq);
            return toData;


        }


        System.out.println("昵称+用户名" + nickname+","+username);
        QueryWrapper<Userinfo> userinfoQueryWrapper = new QueryWrapper<>();
        if(!ValidatorUtils.isNullOrEmpty(nickname)){
            userinfoQueryWrapper.like("nickname",nickname).or();
        }
        if(!ValidatorUtils.isNullOrEmpty(username)){
            userinfoQueryWrapper.like("username",username);
        }

        IPage<Userinfo> userIPage = new Page<Userinfo>(current,size);
        IPage<Userinfo> userinfoIPage = userinfoMapper.selectPage(userIPage, userinfoQueryWrapper);

        System.out.println(userinfoIPage == userinfoIPage);

        System.out.println("当前页"+userinfoIPage.getCurrent());
        System.out.println("总页数"+userinfoIPage.getPages());
        System.out.println("总记录数" + userinfoIPage.getTotal());
        System.out.println("每页显示条数"+userinfoIPage.getSize());

        List<Userinfo> userinfos = userinfoIPage.getRecords();

        if(userinfos != null && userinfos.size() != 0){
            for (Userinfo uo : userinfos){
                uo.setUser(userMapper.selectById(uo.getUserId()));
                uo.setCoachInfo(query().eq("user_id",uo.getUserId()).one());
            }
        }

        toData.setData(userinfos);
        toData.SetPageByMybtisPlus(userinfoIPage);

        return toData;
    }

    @Override
    public void insertAccountMoney(int userId, String accountName) {
        QueryWrapper<Userinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        Userinfo one = userinfoMapper.selectOne(wrapper);
        Account account = new Account();
        account.setAcccountName(accountName);
        account.setMoney(0.0);
        account.setStatus(1);
        accountMapper.insert(account);
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.eq("acccount_name",accountName);
        System.out.println("打印："+accountName);
        Account one1 = accountMapper.selectOne(accountQueryWrapper);

        one.setAcccountId(one1.getAcccountId());
        userinfoMapper.updateById(one);
    }

    @Override
    public Account selectAccount(int userId) {
        QueryWrapper<Userinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        Userinfo one = userinfoMapper.selectOne(wrapper);
        if(one==null){
            return null;
        }
        Integer id = one.getAcccountId();

        Account account = accountMapper.selectById(id);
        return account;
    }










    //动态查询教练
    @Override
    public List<Map<String, Object>> slectCaoch(Boolean value1,Integer money,Integer cid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("value1",value1);
        map.put("money",money);
        map.put("cid",cid);
        List<Map<String, Object>> list = coachInfoMapper.selectCoach(map);
        return list;
    }
    @Autowired
    private OrdersMapper ordersMapper;
    @Override
    public int selectCount(Integer uid) {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("coach_id",uid);
        Integer count = ordersMapper.selectCount(wrapper);
        return count;
    }

    @Override
    public List<Map<String, Object>> selectAllCoach() {
        List<Map<String, Object>> maps = coachInfoMapper.selectAllCoach();
        return maps;
    }

}
